package com.hdcircles.autoform.presenter;

import android.widget.TextView;

import com.hdcircles.autoform.app.Constants;
import com.hdcircles.autoform.base.RxPresenter;
import com.hdcircles.autoform.model.bean.FormBean;
import com.hdcircles.autoform.model.bean.User;
import com.hdcircles.autoform.model.http.HttpResponse;
import com.hdcircles.autoform.model.http.RetrofitHelper;
import com.hdcircles.autoform.presenter.contract.FormListContract;
import com.hdcircles.autoform.util.LogUtil;
import com.hdcircles.autoform.util.RxUtil;
import com.hdcircles.autoform.util.SharedPreferenceUtil;

import java.util.List;

import javax.inject.Inject;

import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by hdcircles on 2016/10/18.
 */

public class FormListPresenter extends RxPresenter<FormListContract.View> implements FormListContract.Presenter {

    private RetrofitHelper retrofitHelper;

    @Inject
    public FormListPresenter(RetrofitHelper retrofitHelper){
        this.retrofitHelper=retrofitHelper;
    }

    @Override
    public void getFormData(String id,String email,boolean isCreator) {
//        List<FormBean> formBeanList=realmHelper.getDoneFormList(id);
//        Logger.json(new Gson().toJson(formBeanList));
//        mView.showContent(formBeanList);
        FormBean params=new FormBean();
        params.setFormId(id);
        params.setStatus(Constants.STATUS_DONE);
        if (isCreator){
            params.setCreator(email);
        }else {
            params.setWriter(email);
        }
        Subscription subscription=retrofitHelper.getFormList(params)
                .compose(RxUtil.<HttpResponse<List<FormBean>>>rxScheduleHelper())
                .compose(RxUtil.<List<FormBean>>handleResult())
                .subscribe(new Action1<List<FormBean>>() {
                    @Override
                    public void call(List<FormBean> formBeanList) {
                        mView.showContent(formBeanList);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtil.d(throwable.getMessage());
                        mView.showError(throwable.getMessage());
                    }
                });
        addSubscribe(subscription);
    }

    @Override
    public void getUser(String email, final String time, final TextView textView) {
        Subscription subscription=retrofitHelper.getUser(email)
                .compose(RxUtil.<HttpResponse<User>>rxScheduleHelper())
                .compose(RxUtil.<User>handleResult())
                .subscribe(new Action1<User>() {
                    @Override
                    public void call(User user) {
                        mView.setUserName(time,user.getName(),textView);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.showError(throwable.getMessage());
                    }
                });
        addSubscribe(subscription);
    }


}
